package com.ai.service.impl;

import com.ai.entity.SysDictDetailEntity;
import com.ai.entity.SysDictEntity;
import com.ai.exception.BusinessException;
import com.ai.mapper.SysDictDetailMapper;
import com.ai.mapper.SysDictMapper;
import com.ai.service.SysDictDetailService;
import com.ai.utils.ConvertUtils;
import com.ai.web.PageResp;
import com.ai.web.request.DictDetailAddReq;
import com.ai.web.request.DictDetailPage;
import com.ai.web.request.DictDetailUpdateReq;
import com.ai.web.response.SysDictDetailResp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据字典服务
 */
@Service("sysDictDetailService")
public class SysDictDetailServiceImpl implements SysDictDetailService {
    @Resource
    private SysDictDetailMapper sysDictDetailMapper;
    @Resource
    private SysDictMapper sysDictMapper;


    @Override
    public PageResp<SysDictDetailResp> listByPage(DictDetailPage dictDetailPage) {

        SysDictEntity sysDictEntity = sysDictMapper.selectById(dictDetailPage.getDictId());
        if (sysDictEntity == null) {
            throw new BusinessException("获取字典数据失败!");
        }

        LambdaQueryWrapper<SysDictDetailEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysDictDetailEntity::getDictId, dictDetailPage.getDictId());
        wrapper.orderByAsc(SysDictDetailEntity::getSort);
        IPage<SysDictDetailEntity> result = sysDictDetailMapper.selectPage(new Page<>(dictDetailPage.getPage(),dictDetailPage.getLimit()), wrapper);

        return ConvertUtils.convertPageResp(result, e -> {
            SysDictDetailResp convert = ConvertUtils.convert(e, SysDictDetailResp.class);
            convert.setDictName(sysDictEntity.getName());
            return convert;
        });
    }

    @Override
    public void save(DictDetailAddReq dictDetailAddReq) {

        LambdaQueryWrapper<SysDictDetailEntity> sysDictDetailEntityLambdaQueryWrapper = Wrappers.<SysDictDetailEntity>lambdaQuery().eq(SysDictDetailEntity::getValue, dictDetailAddReq.getValue())
                .eq(SysDictDetailEntity::getDictId, dictDetailAddReq.getDictId());
        SysDictDetailEntity sysDictDetailEntity = sysDictDetailMapper.selectOne(sysDictDetailEntityLambdaQueryWrapper);
        if(null!=sysDictDetailEntity){
            throw new BusinessException("字典名称-字典值已存在");
        }

        SysDictDetailEntity convert = ConvertUtils.convert(dictDetailAddReq, SysDictDetailEntity.class);
        sysDictDetailMapper.insert(convert);
    }

    @Override
    public void deleteByIds(List<String> ids) {
        sysDictDetailMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(DictDetailUpdateReq dictDetailUpdateReq) {
        SysDictDetailEntity sysDictDetailEntity = sysDictDetailMapper.selectById(dictDetailUpdateReq.getId());
        if(null!=sysDictDetailEntity){
            sysDictDetailEntity.setDictId(dictDetailUpdateReq.getDictId());
            sysDictDetailEntity.setSort(dictDetailUpdateReq.getSort());
            sysDictDetailEntity.setLabel(dictDetailUpdateReq.getLabel());
            sysDictDetailEntity.setValue(dictDetailUpdateReq.getValue());
            sysDictDetailMapper.updateById(sysDictDetailEntity);
        }
    }
}